import { login, getuserinfo, getphoto } from '@/api/user.js'
import { getToken, setToken, removeToken, settimetoken } from '@/utils/auth.js'
export default {
  namespaced: true,
  state: {
    // 获取本地存储中的token
    // token: localStorage.getItem('token') || ''
    token: getToken() || '',
    userinfo: {}
  },
  getters: {},
  mutations: {
    // 存储用户信息
    getuserinfo(state, payload) {
      state.userinfo = payload
    },
    // 删除用户信息
    deluserinfo(state) {
      state.userinfo = {}
    },
    // 将数据存储到vuex当中
    setToken(state, payload) {
      state.token = payload
      // 设置token到本地存储
      // localStorage.setItem('token', payload)
      setToken(payload)
    },
    // 删除数据
    delToken(state) {
      state.token = ''
      // 删除token
      // localStorage.removeItem('token')
      removeToken()
    }
  },
  actions: {
    // 封装删除token 删除用户信息
    loginout(context) {
      context.commit('delToken')
      context.commit('deluserinfo')
    },
    // 登录
    async UserLogin(context, loginForm) {
      // 发送请求,,
      const data = await login(loginForm)
      console.log(data)
      // 设置时间戳

      // 存储token
      context.commit('setToken', data.data)
      settimetoken()
      console.log(1111)
    },
    // 获取详情
    async setuserinfo(context) {
      const { data } = await getuserinfo()
      console.log(data)
      const res = await getphoto(data.userId)
      console.log(res)
      context.commit('getuserinfo', { ...data, ...res.data })
    }
  },
  modules: {}
}
